/**
* Javascript file for sources needed at userinfo.php
*/

dojo.require("dojox.grid.EnhancedGrid");
dojo.require("dojox.grid.enhanced.plugins.Pagination");
dojo.require("dojo.parser");

var stats_show_tooltip = false;
var stats_tooltip_requested = false;
var stats_current_timeout = null;
var stats_graph_buffer = {};
var stats_first_tab = null;
var stats_suffix = "1";

function galaxytool_showTooltip(e) {
	if (e.cellIndex < 0) return;

	if (e.cellIndex == 0) {
		var value = e.grid.store.getValue(e.grid.getItem(e.rowIndex),"username");
		value += " @ " + e.grid.store.getValue(e.grid.getItem(e.rowIndex),"last_update");
		dijit.showTooltip(value, e.cellNode);
	}

}

function galaxytool_hideTooltip(e) {
	dijit.hideTooltip(e.cellNode);
}

function galaxytool_centered_formatter(val, rowIdx, cell) {
	cell.customClasses.push("centered_text");
	return val;
}

function galaxytool_pstats_rank1(val, rowIdx, cell) {
	return galaxytool_stats_rank(val, rowIdx, cell, playergrid1, playerstore1, "p_status");
}
function galaxytool_pstats_rank2(val, rowIdx, cell) {
	return galaxytool_stats_rank(val, rowIdx, cell, playergrid2, playerstore2, "p_status");
}
function galaxytool_astats_rank1(val, rowIdx, cell) {
	return galaxytool_stats_rank(val, rowIdx, cell, allygrid1, allystore1, "a_status");
}
function galaxytool_astats_rank2(val, rowIdx, cell) {
	return galaxytool_stats_rank(val, rowIdx, cell, allygrid2, allystore2, "a_status");
}
function galaxytool_stats_rank(val, rowIdx, cell, grid, store, status) {
	cell.customClasses.push("centered_text");
	var status_value = store.getValues(grid.getItem(rowIdx),status);
	if (status_value != "nothing") {
		cell.customClasses.push("bg_"+status);
	}
	return val;
}

function galaxytool_pstats_name1(val, rowIdx, cell) {
	return galaxytool_pstats_name(val, rowIdx, cell, playergrid1, playerstore1, "p_status");
}
function galaxytool_pstats_name2(val, rowIdx, cell) {
	return galaxytool_pstats_name(val, rowIdx, cell, playergrid2, playerstore2, "p_status");
}

function galaxytool_pstats_name(val, rowIdx, cell, grid, store, status) {
	cell.customClasses.push("centered_text");
	cell.customClasses.push("hyperlink");
	cell.customClasses.push("stats_text");
	var p_status = store.getValues(grid.getItem(rowIdx),status);

	var id = store.getValues(grid.getItem(rowIdx),"id");
	var url = "playerinformation.php?id="+id;

	val = "<a href=\""+url+"\" class=\""+p_status+"\">"+val+"</a>";
	return val;
}

function galaxytool_astats_name1(val, rowIdx, cell) {
	return galaxytool_astats_name(val, rowIdx, cell, allygrid1, allystore1);
}
function galaxytool_astats_name2(val, rowIdx, cell) {
	return galaxytool_astats_name(val, rowIdx, cell, allygrid2, allystore2);	
}

function galaxytool_astats_name(val, rowIdx, cell, grid, store) {
	cell.customClasses.push("centered_text");
	cell.customClasses.push("hyperlink");
	cell.customClasses.push("stats_text");
	var a_status = store.getValues(grid.getItem(rowIdx),"a_status");
	
	if (a_status == "") return val;

	
	var id = store.getValues(grid.getItem(rowIdx),"id");
	if (id == 0) return "<span class=\""+a_status+"\">"+val+"</span>";
	
	var url = "allyinformation.php?id="+id;
	val = "<a href=\""+url+"\" class=\""+a_status+"\">"+val+"</a>";

	return val;
}

function galaxytool_pstats_ally1(val, rowIdx, cell) {
	return galaxytool_pstats_ally(val, rowIdx, cell, playergrid1, playerstore1, "a_status");
}
function galaxytool_pstats_ally2(val, rowIdx, cell) {
	return galaxytool_pstats_ally(val, rowIdx, cell, playergrid2, playerstore2, "a_status");
}

function galaxytool_pstats_ally(val, rowIdx, cell, grid, store, status) {
	cell.customClasses.push("centered_text");
	cell.customClasses.push("hyperlink");
	cell.customClasses.push("stats_text");
	var a_status = store.getValues(grid.getItem(rowIdx),status);
	
	if (a_status == "") return val;

	var id = store.getValues(grid.getItem(rowIdx),"a_id");
	if (id == 0) return "<span class=\""+a_status+"\">"+val+"</span>";

	var url = "allyinformation.php?id="+id;
	val = "<a href=\""+url+"\" class=\""+a_status+"\">"+val+"</a>";

	return val;
}

function galaxytool_pstats_status(val, rowIdx, cell) {
	var char = "";
	var new_val = "";
	for (var i=0; i<val.length; i++) {
		char = val.substr(i,1);

		switch(char) {
			case stats_banned:
			new_val += "<span class=\"banned\">"+stats_banned+"</span>";
			break;
			case stats_vacation:
			new_val += "<span class=\"vacation\">"+stats_vacation+"</span>";
			break;
			case stats_noob:
			new_val += "<span class=\"noob\">"+stats_noob+"</span>";
			break;
			case stats_inactive:
			new_val += "<span class=\"inactive\">"+stats_inactive+"</span>";
			break;
			case stats_long_inactive:
			new_val += "<span class=\"longinactive\">"+stats_long_inactive+"</span>";
			break;
			case stats_outlaw:
			new_val += "<span class=\"outlaw\">"+stats_outlaw+"</span>";
			break;
		}
	}

	return new_val;
}

function galaxytool_stats_score(val, rowIdx, cell) {
	cell.customClasses.push("centered_text");
	cell.customClasses.push("stats_text");
	return NumberFormat(val);
}
function galaxytool_pstats_research1(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, playergrid1, playerstore1, "rrank");
}
function galaxytool_pstats_research2(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, playergrid2, playerstore2, "rrank");
}
function galaxytool_pstats_fleet1(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, playergrid1, playerstore1, "frank");
}
function galaxytool_pstats_fleet2(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, playergrid2, playerstore2, "frank");
}
function galaxytool_pstats_economy1(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, playergrid1, playerstore1, "erank");
}
function galaxytool_pstats_economy2(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, playergrid2, playerstore2, "erank");
}
function galaxytool_pstats_honour1(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, playergrid1, playerstore1, "hrank");
}
function galaxytool_pstats_honour2(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, playergrid2, playerstore2, "hrank");
}
function galaxytool_pstats_md1(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, playergrid1, playerstore1, "fdrank");
}
function galaxytool_pstats_md2(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, playergrid2, playerstore2, "fdrank");
}
function galaxytool_pstats_ml1(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, playergrid1, playerstore1, "flrank");
}
function galaxytool_pstats_ml2(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, playergrid2, playerstore2, "flrank");
}


function galaxytool_astats_research1(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, allygrid1, allystore1, "rrank");
}
function galaxytool_astats_research2(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, allygrid2, allystore2, "rrank");
}
function galaxytool_astats_fleet1(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, allygrid1, allystore1, "frank");
}
function galaxytool_astats_fleet2(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, allygrid2, allystore2, "frank");
}
function galaxytool_astats_honour1(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, allygrid1, allystore1, "hrank");
}
function galaxytool_astats_honour2(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, allygrid2, allystore2, "hrank");
}
function galaxytool_astats_md1(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, allygrid1, allystore1, "fdrank");
}
function galaxytool_astats_md2(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, allygrid2, allystore2, "fdrank");
}
function galaxytool_astats_ml1(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, allygrid1, allystore1, "flrank");
}
function galaxytool_astats_ml2(val, rowIdx, cell) {
	return galaxytool_stats(val, rowIdx, cell, allygrid2, allystore2, "flrank");
}
function galaxytool_stats(val, rowIdx, cell, grid, store, rank_key) {
	cell.customClasses.push("centered_text");
	cell.customClasses.push("stats_text");
	var rank = store.getValues(grid.getItem(rowIdx),rank_key);
	val = NumberFormat(val);
	return val+" ("+rank+")";
}

function galaxytool_compare_float (a,b) {
	if (parseFloat(a) < parseFloat(b)){
		return -1;
	} else if (parseFloat(a) > parseFloat(b)) {
		return 1;
	} else {
		return 0;
	}
}

function galaxytool_load_new_stats(type, suffix, grid, store) {
	// get dijit settings for player status
	var inactive = dijit.byId("select_inactive"+suffix).get("value");
	var vacation = dijit.byId("select_vacation"+suffix).get("value");
	var noob     = dijit.byId("select_noob"+suffix).get("value");

	var store_url = "ajax/ajax_stats.php?type="+type;
	if (inactive == "inactive") store_url += "&inactive=i";
	else if (inactive == "long_inactive") store_url += "&inactive=I";
	else if (inactive == "yes") store_url += "&inactive=true";
	else if (inactive == "no") store_url += "&inactive=false";

	if (vacation == "yes") store_url += "&vacation=true";
	else if (vacation == "no") store_url += "&vacation=false";

	if (noob == "yes") store_url += "&noob=true";
	else if (noob == "no") store_url += "&noob=false";

	// reload content from URL
	store.url = store_url;
	store.close();
	grid.setStore(store);
}

function galaxytool_playerstats_select_row(rowIndex) {
	dijit.byId("playerstatus_button"+stats_suffix).set('disabled', false);
	try {
		dijit.byId("delete_player_button"+stats_suffix).set('disabled', false);
	} catch(no_button) { }
}

function galaxytool_playerstats_unselect_row(rowIndex) {
	dijit.byId("playerstatus_button"+stats_suffix).set('disabled', true);
	try {
		dijit.byId("delete_player_button"+stats_suffix).set('disabled', true);
	} catch(no_button) { }
}

function galaxytool_allystats_select_row(rowIndex) {
	dijit.byId("details_button"+stats_suffix).set('disabled', false);
	try {
		dijit.byId("allystatus_button"+stats_suffix).set('disabled', false);
		dijit.byId("delete_ally_button"+stats_suffix).set('disabled', false);
	} catch(no_button) { }
}

function galaxytool_allystats_unselect_row(rowIndex) {
	dijit.byId("details_button"+stats_suffix).set('disabled', true);
	try {
		dijit.byId("allystatus_button"+stats_suffix).set('disabled', true);
		dijit.byId("delete_ally_button"+stats_suffix).set('disabled', true);
	} catch(no_button) { }
}

function galaxytool_playerstatus(NewStatus, grid, store) {

	var player_id  = grid.store.getValue(grid.selection.getFirstSelected(), "id");
	var source_url = "ajax/ajax_stats.php?type=change_player&player_id="+player_id+"&new_status="+NewStatus;
	var xhrArgs = {
	        url: source_url,
	        handleAs: "json",
	        load: function(jsonData) {
	        	if (jsonData.messages != null) {
	        		showMessages(jsonData.messages);
	        	} else {
	        		// reload of grid needed
					store.close();
					grid.setStore(store);
	        	}

	        },
	        error: function(error) {
				console.log("Error:");
				console.log(error);
	        }
	}	
	dojo.xhrPost(xhrArgs);
}

function galaxytool_delete_player(grid, store) {
	var confirmed   = confirm(stats_confirm_delete);
	if (confirmed != true) return;

	var player_id  = grid.store.getValue(grid.selection.getFirstSelected(), "id");
	var source_url = "ajax/ajax_stats.php?type=player_delete&player_id="+player_id;
	var xhrArgs = {
	        url: source_url,
	        handleAs: "json",
	        load: function(jsonData) {
	        	if (jsonData.messages != null) {
	        		showMessages(jsonData.messages);
	        	} else {
	        		// reload of grid needed
					store.close();
					grid.setStore(store);
	        	}

	        },
	        error: function(error) {
				console.log("Error:");
				console.log(error);
	        }
	}	
	dojo.xhrPost(xhrArgs);
}

function galaxytool_allystatus(NewStatus, grid, store) {
	var alliance_id = grid.store.getValue(grid.selection.getFirstSelected(), "id");
	var source_url = "ajax/ajax_stats.php?type=change_alliance&alliance_id="+alliance_id+"&new_status="+NewStatus;
	var xhrArgs = {
	        url: source_url,
	        handleAs: "json",
	        load: function(jsonData) {
	        	if (jsonData.messages != null) {
	        		showMessages(jsonData.messages);
	        	} else {
	        		// reload of grid needed
					store.close();
					grid.setStore(store);
	        	}

	        },
	        error: function(error) {
				console.log("Error:");
				console.log(error);
	        }
	}	
	dojo.xhrPost(xhrArgs);

}

function galaxytool_delete_ally(grid, store) {
	var confirmed   = confirm(stats_confirm_delete);
	if (confirmed != true) return;
	
	var alliance_id = grid.store.getValue(grid.selection.getFirstSelected(), "id");
	var source_url  = "ajax/ajax_stats.php?type=alliance_delete&alliance_id="+alliance_id;

	var xhrArgs = {
	        url: source_url,
	        handleAs: "json",
	        load: function(jsonData) {
	        	if (jsonData.messages != null) {
	        		showMessages(jsonData.messages);
	        	} else {
	        		// reload of grid needed
					store.close();
					grid.setStore(store);
	        	}

	        },
	        error: function(error) {
				console.log("Error:");
				console.log(error);
	        }
	}	
	dojo.xhrPost(xhrArgs);	
}

function galaxytool_ally_info(suffix, grid, store) {

	var alliance_id = grid.store.getValue(grid.selection.getFirstSelected(), "id");
	var source_url  = "ajax/ajax_stats.php?type=graph&id="+alliance_id;

	var content = stats_graph_buffer["ag_"+alliance_id]
	if(typeof(content) !== 'undefined' && content != null) {
		var dialog = dijit.byId("AllyDetailsDialog");
		dialog.set("content", content);
		dialog.show();
		return;
	}
	var xhrArgs = {
	        url: source_url,
	        handleAs: "text",
	        load: function(textData) {
	        	stats_graph_buffer["ag_"+alliance_id] = textData;
				var dialog = dijit.byId("AllyDetailsDialog"+suffix);
				dialog.set("content", textData);
				dialog.show();

	        },
	        error: function(error) {
				console.log("Error:");
				console.log(error);
	        }
	}	
	dojo.xhrPost(xhrArgs);

}

function galaxytool_player_tab_loaded(data) {
	dojo.connect(playergrid1, "onRowMouseOver", galaxytool_showTooltip);
	dojo.connect(playergrid1, "onRowMouseOut", galaxytool_hideTooltip);
	dojo.connect(playergrid1, "onSelected", galaxytool_playerstats_select_row);
	dojo.connect(playergrid1, "onDeselected", galaxytool_playerstats_unselect_row);
	//dojo.connect(playergrid1, "onCellClick", galaxytool_pstats_cellclick);
}
function galaxytool_player_tab_b_loaded(data) {
	dojo.connect(playergrid2, "onRowMouseOver", galaxytool_showTooltip);
	dojo.connect(playergrid2, "onRowMouseOut", galaxytool_hideTooltip);
	dojo.connect(playergrid2, "onSelected", galaxytool_playerstats_select_row);
	dojo.connect(playergrid2, "onDeselected", galaxytool_playerstats_unselect_row);
	//dojo.connect(playergrid2, "onCellClick", galaxytool_pstats_cellclick);
}

function galaxytool_ally_tab_loaded(data) {
	dojo.connect(allygrid1, "onRowMouseOver", galaxytool_showTooltip);
	dojo.connect(allygrid1, "onRowMouseOut", galaxytool_hideTooltip);
	dojo.connect(allygrid1, "onSelected", galaxytool_allystats_select_row);
	dojo.connect(allygrid1, "onDeselected", galaxytool_allystats_unselect_row);
	//dojo.connect(allygrid1, "onCellClick", galaxytool_astats_cellclick);
}
function galaxytool_ally_tab_b_loaded(data) {
	dojo.connect(allygrid2, "onRowMouseOver", galaxytool_showTooltip);
	dojo.connect(allygrid2, "onRowMouseOut", galaxytool_hideTooltip);
	dojo.connect(allygrid2, "onSelected", galaxytool_allystats_select_row);
	dojo.connect(allygrid2, "onDeselected", galaxytool_allystats_unselect_row);
	//dojo.connect(allygrid2, "onCellClick", galaxytool_astats_cellclick);
}

function galaxytool_stats_init() {
	// select user defined tab
	var choosen_tab = window.location.hash;
	stats_first_tab = choosen_tab;
	if (choosen_tab != "") {
		try {
			choosen_tab = choosen_tab.replace(/#select_/, "");
			dijit.byId("tabcontainer").selectChild(dijit.byId(choosen_tab));
		} catch (ignore) {
			console.log("error: ");
			console.log(ignore);
			// select default tab in case of wrong user input
		}
	}

	dojo.subscribe("tabcontainer-selectChild", function(child) {
		var tab_id = child.get("id");
		window.location.hash = "#select_" + tab_id;
		
		// the first grid is not correctly rendered if it loaded "invisible"
		// re-render it once if required: row selector is too small then (dojo bug)
		if (tab_id == "tab1" && stats_first_tab != null && stats_first_tab != "tab1") {
			dijit.byId("playergrid1").render();
			stats_first_tab = null;
		}
	});

	// dojo.connect(dijit.byId("user_select"), "onChange", galaxytool_load_new_IP ); <-- connection is created on TAB load
	dojo.connect(dijit.byId("tab1"), "onLoad", galaxytool_player_tab_loaded );
	dojo.connect(dijit.byId("tab1b"), "onLoad", galaxytool_player_tab_b_loaded );
	dojo.connect(dijit.byId("tab2"), "onLoad", galaxytool_ally_tab_loaded );
	dojo.connect(dijit.byId("tab2b"), "onLoad", galaxytool_ally_tab_b_loaded );
}

dojo.ready(galaxytool_stats_init);